//
//  UIView+LDCategory.m
//  Expecta
//
//  Created by sishengxiu on 2017/11/8.
//

#import "UIView+LDCategory.h"

@implementation UIView (LDCategory)
#pragma mark 视图坐标大小

- (CGPoint)origin
{
    return self.frame.origin;
}

- (CGFloat)minX
{
    return self.frame.origin.x;
}

- (CGFloat)maxX
{
    return self.frame.origin.x + self.frame.size.width;
}
- (CGFloat)minY
{
    return self.frame.origin.y;
}
- (CGFloat)maxY
{
    return self.frame.origin.y + self.frame.size.height;
}

- (CGSize)size
{
    return self.frame.size;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

#pragma mark 隐藏键盘
- (void)hideKeyboard
{
    UIDevice
    if (self.isFirstResponder)
    {
        [self resignFirstResponder];
        return;
    }
    for (UIView *subView in self.subviews)
    {
        [subView hideKeyboard];
    }
}

#pragma mark 视图阴影渲染
- (void)render:(CGFloat)radius withColor:(UIColor*)color
{
    self.layer.shadowOffset = CGSizeMake(radius, radius);
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOpacity = radius;
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
}

#pragma mark 视图截取成图片
- (UIImage*)viewShot
{
    UIGraphicsBeginImageContext(self.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, self.center.x, self.center.y);
    CGContextConcatCTM(context, [self transform]);
    CGContextTranslateCTM(context, -self.size.width * [self.layer anchorPoint].x, -self.size.height * [[self layer] anchorPoint].y);
    [self.layer renderInContext:context];
    CGContextRestoreGState(context);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark 圆角/边框
- (void)setCornerRadius:(CGFloat)radius
{
    [self.layer setMasksToBounds:YES];
    [self.layer setCornerRadius:radius];
}

- (void)setBorder:(CGFloat)width withColor:(UIColor *)color
{
    [self.layer setBorderColor:color.CGColor];
    [self.layer setBorderWidth:width];
}


@end
